<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="选民姓名" prop="voterName">
        <el-input
          v-model="queryParams.voterName"
          placeholder="请输入选民姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="代表姓名" prop="representativeId">
        <el-input
          v-model="queryParams.representativeId"
          placeholder="请输入标识联系的代表名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入意见或建议的标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="建议状态" prop="voterStatus">
        <el-select v-model="queryParams.voterStatus" placeholder="请选择状态"  @keyup.enter.native="handleQuery">
          <el-option label="未处理"  value="0"> </el-option>
          <el-option label="代办"  value="1"> </el-option>
          <el-option label="办结" value="2"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['PublicOpinion:PublicOpinion:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['PublicOpinion:PublicOpinion:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['PublicOpinion:PublicOpinion:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['PublicOpinion:PublicOpinion:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="PublicOpinionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="选民姓名" align="center" prop="voterName" />
      <el-table-column label="选民电话" align="center" prop="contactInfo" />
      <el-table-column label="联系对象" align="center" prop="contactPersonId" />
      <el-table-column label="所属街道" align="center" prop="constituencyId" />
      <el-table-column label="指定代表" align="center" prop="representativeId" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="内容" align="center" prop="content" />
      <el-table-column label="办理时间" align="center" prop="feedbackTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.feedbackTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="反馈内容" align="center" prop="feedbackContent" />
      <el-table-column label="建议状态" align="center" prop="voterStatus" />
      <el-table-column label="提交时间" align="center" prop="submitTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.submitTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['PublicOpinion:PublicOpinion:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['PublicOpinion:PublicOpinion:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改民情动态对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="选民姓名" prop="voterName">
          <el-input v-model="form.voterName" placeholder="请输入记录选民的姓名" />
        </el-form-item>
        <el-form-item label="选民电话" prop="contactInfo">
          <el-input v-model="form.contactInfo" placeholder="请输入记录选民的联系方式" />
        </el-form-item>
        <el-form-item label="所属街道" prop="constituencyId">
          <el-input v-model="form.constituencyId" placeholder="请输入标识意见所属的选区" />
        </el-form-item>
        <el-form-item label="联系代表" prop="representativeId">
          <el-select v-model="form.representativeId"  placeholder="请输入标识联系的代表">
            <el-option :label="item.name" :key="item.id" :value="item.id" v-for="item in roleuserinfoList"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="建议标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入意见或建议的标题" />
        </el-form-item>
<!--        <el-form-item label="建议的材料" prop="material">-->
<!--          <file-upload v-model="form.material"/>-->
<!--        </el-form-item>-->
        <el-form-item label="建议的内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="反馈时间" prop="feedbackTime">
          <el-date-picker clearable
            v-model="form.feedbackTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择记录反馈给选民的时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="反馈内容">
          <editor v-model="form.feedbackContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="建议状态" prop="voterStatus">
          <el-select v-model="form.voterStatus" placeholder="请选择状态"  @keyup.enter.native="handleQuery">
            <el-option label="未处理" key="0" value="0"> </el-option>
            <el-option label="待办"  value="1"> </el-option>
            <el-option label="办结" value="2"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="提交时间" prop="submitTime">
          <el-date-picker clearable
            v-model="form.submitTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择记录意见提交的时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="办结时间" prop="processingTime">
          <el-date-picker clearable
            v-model="form.processingTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择记录处理完成的时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPublicOpinion, getPublicOpinion, delPublicOpinion, addPublicOpinion, updatePublicOpinion } from "@/api/PublicOpinion/PublicOpinion";
import {listUserinfoByRole} from "@/api/userinfo/userinfo";

export default {
  name: "PublicOpinion",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 民情动态表格数据
      PublicOpinionList: [],
      roleuserinfoList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        voterName: null,
        representativeId: null,
        title: null,
        voterStatus: null,
        role: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getListByRole();
  },
  methods: {
    /** 查询民情动态列表 */
    getList() {
      this.loading = true;
      listPublicOpinion(this.queryParams).then(response => {
        this.PublicOpinionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //根据用户角色查询用户
    getListByRole() {
      this.queryParams.role=2
      listUserinfoByRole(this.queryParams).then(response => {
        this.roleuserinfoList = response.data;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        opinionId: null,
        voterId: null,
        voterName: null,
        contactInfo: null,
        constituencyId: null,
        representativeId: null,
        title: null,
        material: null,
        content: null,
        feedbackTime: null,
        feedbackContent: null,
        voterStatus: null,
        contactPersonId: null,
        approvalFeedback: null,
        submitTime: null,
        processingStatus: null,
        processingOpinion: null,
        processingTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.opinionId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加民情动态";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const opinionId = row.opinionId || this.ids
      getPublicOpinion(opinionId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改民情动态";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.opinionId != null) {
            updatePublicOpinion(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPublicOpinion(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const opinionIds = row.opinionId || this.ids;
      this.$modal.confirm('是否确认删除民情动态编号为"' + opinionIds + '"的数据项？').then(function() {
        return delPublicOpinion(opinionIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('PublicOpinion/PublicOpinion/export', {
        ...this.queryParams
      }, `PublicOpinion_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
